package com.yanqu.road.dao.impl.drugstore;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.drugstore.config.DrugStoreBadgeInfo;
import com.yanqu.road.entity.drugstore.config.DrugStoreDrugUpgradeInfo;
import com.yanqu.road.entity.drugstore.config.DrugStorePrescriptionDispenseInfo;
import com.yanqu.road.entity.drugstore.config.DrugStorePrescriptionInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DrugStoreConfigDaoImpl extends TempDao {

    private static String getDrugStoreBadgeInfoSql = "select * from `t_p_badge` where `type`=2 ORDER BY `id` ASC";

    public List<DrugStoreBadgeInfo> getDrugStoreBadgeInfoList() {
        List<DrugStoreBadgeInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getDrugStoreBadgeInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                DrugStoreBadgeInfo data = new DrugStoreBadgeInfo();
                data.setId(rs.getInt("id"));
                data.setLevel(rs.getInt("level"));
                data.setUnlockValue(rs.getInt("unlockValue"));
                data.setSkillsList(StringUtils.stringToIntegerList(rs.getString("skillsList"), "\\|"));
                data.setSpecialEffect(rs.getString("specialEffect"));
                data.setExParam(rs.getString("exParam"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getDrugStoreDrugUpgradeInfoSql = "select * from `t_p_drug_upgrade` ORDER BY `id` ASC";

    public List<DrugStoreDrugUpgradeInfo> getDrugStoreDrugUpgradeInfoList() {
        List<DrugStoreDrugUpgradeInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getDrugStoreDrugUpgradeInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                DrugStoreDrugUpgradeInfo data = new DrugStoreDrugUpgradeInfo();
                data.setId(rs.getInt("id"));
                data.setPrescriptionId(rs.getInt("prescriptionId"));
                data.setLevel(rs.getInt("level"));
                data.setNeed(rs.getInt("need"));
                data.setSkillList(StringUtils.stringToIntegerList(rs.getString("skills"), ";"));
                data.setExParam(rs.getString("exParam"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getDrugStorePrescriptionInfoSql = "select * from `t_p_drug_chu_fang` ORDER BY `id` ASC";

    public Map<Integer, DrugStorePrescriptionInfo> getDrugStorePrescriptionInfoMap() {
        Map<Integer, DrugStorePrescriptionInfo> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getDrugStorePrescriptionInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                DrugStorePrescriptionInfo data = new DrugStorePrescriptionInfo();
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setMake(rs.getString("make"));
                data.setMakeLv(rs.getInt("makeLv"));
                data.setGetExp(rs.getString("getExp"));
                data.setRewards(rs.getString("rewards"));
                data.setUnLock(rs.getInt("unLock"));
                data.setDialoguePeople(rs.getString("dialoguePeople"));
                data.setExParam(rs.getString("exParam"));
                data.setFirstReward(rs.getString("firstReward"));
                if (data.getType() == 2) {
                    // 疑难杂症
                    List<String> list = StringUtils.stringToStringList(data.getMake(), "\\|");
                    // 解析药材
                    List<String> crudeDrugList = StringUtils.stringToStringList(list.get(0), ";");
                    Map<Integer, Integer> crudeDrugMap = new HashMap<>();
                    DrugStorePrescriptionDispenseInfo dispenseInfo = new DrugStorePrescriptionDispenseInfo();
                    for (String str : crudeDrugList) {
                        String[] strings = str.split("=");
                        int crudeDrugId = Integer.valueOf(strings[0]);
                        crudeDrugMap.put(crudeDrugId, Integer.valueOf(strings[1]));
                        dispenseInfo.getCrudeDrugList().add(crudeDrugId);
                    }
                    dispenseInfo.setCrudeDrugMap(crudeDrugMap);
                    dispenseInfo.setDrugIntroductionId(Integer.valueOf(list.get(1)));
                    dispenseInfo.setOrderId(Integer.valueOf(list.get(2)));
                    data.setDispenseInfo(dispenseInfo);
                }
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

}
